//
// Created by 黄扬航 on 2023/4/19.
//
#include "Control.h"
#include "Tools.h"
#include <conio.h>
#include "Point.h"
#include "Menu.h"
#include "Snake.h"
Tools a;
int Control::HradLevel = 100;
int Control::SeletMode() {
    int CursorPositionX = 23, CursorPositionY = 5;
    a.MoveCursorTo(CursorPositionX, CursorPositionY, '>');
    char Operation;
    while (true)
    {
        Operation = _getch();
        switch (Operation) {
            case 72: {
                a.MoveCursorTo(CursorPositionX, CursorPositionY, ' ');
                CursorPositionY == 5 ? CursorPositionY = 7 : CursorPositionY--;
                a.MoveCursorTo(CursorPositionX, CursorPositionY, '>');
            }break;
            case 80: {
                a.MoveCursorTo(CursorPositionX, CursorPositionY, ' ');
                CursorPositionY == 7 ? CursorPositionY = 5 : CursorPositionY++;
                a.MoveCursorTo(CursorPositionX, CursorPositionY, '>');
            }break;
            case 13: {
                return CursorPositionY;
            }
            default:continue;
        }
    }
}
int Control::SeletRetry()
{
    int CursorPositionX = (MaxLength / 3) - 1, CursorPositionY = MaxWidth + 3;
    a.MoveCursorTo(CursorPositionX, CursorPositionY, '>');
    char Operation;
    while (true)
    {
        Operation = _getch();
        switch (Operation) {
            case 72: {
                a.MoveCursorTo(CursorPositionX, CursorPositionY, ' ');
                CursorPositionY == 5 ? CursorPositionY = 7 : CursorPositionY--;
                a.MoveCursorTo(CursorPositionX, CursorPositionY, '>');
            }break;
            case 80: {
                a.MoveCursorTo(CursorPositionX, CursorPositionY, ' ');
                CursorPositionY == 7 ? CursorPositionY = 5 : CursorPositionY++;
                a.MoveCursorTo(CursorPositionX, CursorPositionY, '>');
            }break;
            case 13: {
                return CursorPositionY;
            }
            default:continue;
        }
    }
}
void Control::Game() {
    Menu *Menus=new Menu;
    Snake *_Snake=new Snake;
    Menus->PrintBorder();
    int GameLevel;
    Menus->PrintBeginMenu();
    GameLevel = SeletMode();
    if (GameLevel == 5) HradLevel = 1000;
    if (GameLevel == 6) HradLevel = 500;
    if (GameLevel == 7) HradLevel = 100;
    system("CLS");
    Menus->PrintBorder();
    Point pointer=Point::getInstance();
    _Snake->_Snake;
    while (true) {
        Menus->PrintPlayingMenu();
        Menus->Updata();
        bool NatureMoveIsAlive = false, OperateMoveIsAlive = false, KBIsHit = false;
        _Snake->PrintSnake();
        NatureMoveIsAlive =_Snake->SnakeMove();
        if(!pointer.ThereIsFood) pointer.CreatFood();
        Sleep(HradLevel);
        if (_kbhit()) {
            KBIsHit = true;
            int Operation = _getch();
            if (Operation == 224) Operation = _getch();
            OperateMoveIsAlive =_Snake->SnakeMove(Operation);
        }
        if ((NatureMoveIsAlive && !OperateMoveIsAlive && !KBIsHit) || (!NatureMoveIsAlive && OperateMoveIsAlive && KBIsHit)|| (NatureMoveIsAlive && OperateMoveIsAlive && KBIsHit))
            continue;
        else {
            break;
        }
    }
    Menus->PrintDiedMenu();
    if (SeletRetry() == MaxWidth + 3)
    {
        system("CLS");
        delete Menus, _Snake;
        Snake::CurrentScore = 0, Snake::SnakeSize = 3, pointer.ThereIsFood = false;
        Game();
    }
    else return ;
}
